package com.hss.common.utils;

import java.util.List;


import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * Android 手机网络类型 获取 判断
 * 
 * @author HaoZhiDong
 * 
 *         功能描述:获取当前设备使用网络类型
 */
public class NetManagerUtil {
	private static final String TAG = NetManagerUtil.class.getSimpleName();

	public static final int NETTYPE_CMNET = 3;
	public static final int NETTYPE_CMWAP = 2;
	public static final int NETTYPE_WIFI = 1;
	private static Uri PREFERRED_APN_URI = Uri
			.parse("content://telephony/carriers/preferapn");

	private static Uri PREFERRED_APN_ALL_URI = Uri
			.parse("content://telephony/carriers");

	/**
	 * 获取设备mac地址
	 * 
	 * @param context
	 * @return
	 * @author wangjie
	 */
	public static String getMacAddress(Context context) {
		String macAddress;
		WifiManager wifi = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		macAddress = info.getMacAddress();
		Log.d(TAG, "当前mac地址: " + (null == macAddress ? "null" : macAddress));
		if (null == macAddress) {
			return "";
		}
		macAddress = macAddress.replace(":", "");
		return macAddress;
	}
	
	

	/**
	 * MCC+MNC代码 (SIM卡运营商国家代码和运营商网络代码) 仅当用户已在网络注册时有效, CDMA 可能会无效
	 * 
	 * @return （中国移动：46000 46002, 中国联通：46001,中国电信：46003）
	 */
	public static String getNetworkOperator(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getNetworkOperator();
	}

	/**
	 * 返回移动网络运营商的名字(例：中国联通、中国移动、中国电信) 仅当用户已在网络注册时有效, CDMA 可能会无效
	 * 
	 * @return
	 */
	public static String getNetworkOperatorName(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getNetworkOperatorName();
	}

	/**
	 * 返回移动终端类型 PHONE_TYPE_NONE :0手机制式未知 PHONE_TYPE_GSM :1手机制式为GSM，移动和联通
	 * PHONE_TYPE_CDMA :2手机制式为CDMA，电信 PHONE_TYPE_SIP:3
	 * 
	 * @return
	 */
	public static int getPhoneType(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getPhoneType();
	}

	public static  enum NetType {
		 /**
	     * Unknown network class
	     */
		NETWORK_CLASS_UNKNOWN,
		/**
	     * wifi net work
	     */
		NETWORK_WIFI,
		 /**
	     * "2G" networks
	     */
		NETWORK_CLASS_2_G,
		  /**
	     * "3G" networks
	     */
		NETWORK_CLASS_3_G,
		 /**
	     * "4G" networks
	     */
		NETWORK_CLASS_4_G,
	}

	/**
	 * 在中国，联通的3G为UMTS或HSDPA，移动和联通的2G为GPRS或EGDE，电信的2G为CDMA，电信的3G为EVDO
	 * 
	 * @return 2G、3G、4G、未知四种状态
	 */
	public static NetType getNetWorkClass(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);

		switch (telephonyManager.getNetworkType()) {
		case TelephonyManager.NETWORK_TYPE_GPRS:
		case TelephonyManager.NETWORK_TYPE_EDGE:
		case TelephonyManager.NETWORK_TYPE_CDMA:
		case TelephonyManager.NETWORK_TYPE_1xRTT:
		case TelephonyManager.NETWORK_TYPE_IDEN:
			return NetType.NETWORK_CLASS_2_G;

		case TelephonyManager.NETWORK_TYPE_UMTS:
		case TelephonyManager.NETWORK_TYPE_EVDO_0:
		case TelephonyManager.NETWORK_TYPE_EVDO_A:
		case TelephonyManager.NETWORK_TYPE_HSDPA:
		case TelephonyManager.NETWORK_TYPE_HSUPA:
		case TelephonyManager.NETWORK_TYPE_HSPA:
		case TelephonyManager.NETWORK_TYPE_EVDO_B:
		case TelephonyManager.NETWORK_TYPE_EHRPD:
		case TelephonyManager.NETWORK_TYPE_HSPAP:
			return NetType.NETWORK_CLASS_3_G;

		case TelephonyManager.NETWORK_TYPE_LTE:
			return NetType.NETWORK_CLASS_4_G;

		default:
			return NetType.NETWORK_CLASS_UNKNOWN;
		}
	}

	
	
	/**
	 * 判断当前是否是wifi状态
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isWifiConnected(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo wifiNetworkInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (wifiNetworkInfo.isConnected()) {
			return true;
		}
		return false;
	}

	/**
	 * 检查 当前是否有可用网络
	 * 
	 */
	public static boolean checkNetIsAvailable(Context mContext) {
		boolean flag = false;
		ConnectivityManager cwjManager = (ConnectivityManager) mContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cwjManager.getActiveNetworkInfo() != null)
			flag = cwjManager.getActiveNetworkInfo().isAvailable();
		cwjManager = null;
		mContext = null;
		return flag;
	}
	
	/**
	 * 返回状态：当前的网络链接状态
	 * 
	 * @return 没有网络，2G，3G，4G，WIFI
	 */
	public static NetType getNetWorkStatus(Context context) {
		NetType netWorkType = NetType.NETWORK_CLASS_UNKNOWN;

		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

		if (networkInfo != null && networkInfo.isConnected()) {
			int type = networkInfo.getType();

			if (type == ConnectivityManager.TYPE_WIFI) {
				netWorkType = NetType.NETWORK_WIFI;
			} else if (type == ConnectivityManager.TYPE_MOBILE) {
				netWorkType = getNetWorkClass(context);
			}
		}

		return netWorkType;
	}

	// /**
	// * 获取联网方式：APN 通过context取得ConnectivityManager中的NetworkInfo里关于apn的联网信息
	// *
	// * @param context
	// * @return apn
	// */
	// public static String getAPN(Context context) {
	// String apn = "";
	//
	// ConnectivityManager connectivityManager = (ConnectivityManager) context
	// .getSystemService(Context.CONNECTIVITY_SERVICE);// 获取系统的连接服务
	// NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();//
	// 获取网络的连接情况
	// if (activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
	// // 判断WIFI网
	// Log.e("当前网络类型", "WIFI");
	// } else if (activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
	// // 判断3G网
	// Log.e("当前网络类型", "3G网络");
	// }
	//
	// // // 通过context得到ConnectivityManager连接管理
	// // ConnectivityManager manager = (ConnectivityManager) context
	// // .getSystemService(Context.CONNECTIVITY_SERVICE);
	// // // 通过ConnectivityManager得到NetworkInfo网络信息
	// // NetworkInfo info = manager.getActiveNetworkInfo();
	// // // 获取NetworkInfo中的apn信息
	// // if (info != null) {
	// // apn = info.getExtraInfo();
	// // System.out.println(info.getType());
	// // if (apn == null) {
	// // apn = "取不到移动网络信息";
	// // }
	// // } else {
	// // apn = "取不到网络信息";
	// // }
	// return apn;
	// }

	public static void getAPNList(Context context) {
		String tag = "Main.getAPNList()";

		// current不为空表示可以使用的APN
		String projection[] = { "_id,name,numeric,mcc,mnc,apn,user,server,password,proxy,port,mmsproxy,mmsport,mmsc,authtype,type,current" };
		Cursor cr = context.getContentResolver().query(PREFERRED_APN_ALL_URI,
				projection, null, null, null);

		// List<APN> list = new ArrayList<APN>();

		while (cr != null && cr.moveToNext()) {
			String detail = "ID:" + cr.getString(cr.getColumnIndex("_id"))
					+ "\n" + "name:" + cr.getString(cr.getColumnIndex("name"))
					+ "\n" + "numeric:"
					+ cr.getString(cr.getColumnIndex("numeric")) + "\n"
					+ "mcc:" + cr.getString(cr.getColumnIndex("mcc")) + "\n"
					+ "mnc:" + cr.getString(cr.getColumnIndex("mnc")) + "\n"
					+ "apn:" + cr.getString(cr.getColumnIndex("apn")) + "\n"
					+ "user:" + cr.getString(cr.getColumnIndex("user")) + "\n"
					+ "server:" + cr.getString(cr.getColumnIndex("server"))
					+ "\n" + "password:"
					+ cr.getString(cr.getColumnIndex("password")) + "\n"
					+ "proxy:" + cr.getString(cr.getColumnIndex("proxy"))
					+ "\n" + "port:" + cr.getString(cr.getColumnIndex("port"))
					+ "\n" + "mmsproxy:"
					+ cr.getString(cr.getColumnIndex("mmsproxy")) + "\n"
					+ "mmsport:" + cr.getString(cr.getColumnIndex("mmsport"))
					+ "\n" + "mmsc:" + cr.getString(cr.getColumnIndex("mmsc"))
					+ "\n" + "authtype:"
					+ cr.getString(cr.getColumnIndex("authtype")) + "\n"
					+ "type:" + cr.getString(cr.getColumnIndex("type")) + "\n"
					+ "current:" + cr.getString(cr.getColumnIndex("current"));
			System.out.println(detail);

		}
		context = null;
		if (cr != null)
			cr.close();
		// return list;
	}

	/**
	 * 判断是否连通网络
	 * 
	 * @return
	 */
	public static boolean isConnectNet(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo Mobile = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

		NetworkInfo Wifi = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (Mobile !=null&& Mobile.getState() !=null && Mobile.getState().equals(NetworkInfo.State.DISCONNECTED)
				&& Wifi !=null&& Wifi.getState() !=null &&Wifi.getState().equals(NetworkInfo.State.DISCONNECTED)) {
			return false;
		}
		return true;
	}

	/**
	 * 获取当前网络类型
	 * 
	 * @return 0：没有网络 1：WIFI网络 2：WAP网络 3：NET网络
	 */
	public static int getNetworkType(Context context) {
		int netType = 0;
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null) {
			return netType;
		}
		int nType = networkInfo.getType();
		if (nType == ConnectivityManager.TYPE_MOBILE) {
			String extraInfo = networkInfo.getExtraInfo();
			if (!StringUtils.isEmpty(extraInfo)) {
				if (extraInfo.toLowerCase().equals("cmnet")) {
					netType = NETTYPE_CMNET;
				} else {
					netType = NETTYPE_CMWAP;
				}
			}
		} else if (nType == ConnectivityManager.TYPE_WIFI) {
			netType = NETTYPE_WIFI;
		}
		return netType;
	}

}
